/*
 * Copyright 2015 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package io.github.soiff.mdoc.pom;

import java.util.*;

public interface Pom {

    String NATIVE_MAVEN_PARSER_CLASS = "org.kie.scanner.MavenPomModelGenerator";

    ReleaseId getReleaseId();

    ReleaseId getParentReleaseId();

    Collection<ReleaseId> getDependencies();

    class Internal implements Pom {
        private ReleaseId releaseId;
        private ReleaseId parentReleaseId;
        private final Map<String, Set<ReleaseId>> dependencies = new HashMap<String, Set<ReleaseId>>();

        @Override
        public ReleaseId getReleaseId() {
            return releaseId;
        }

        public void setReleaseId(ReleaseId releaseId) {
            this.releaseId = releaseId;
        }

        @Override
        public ReleaseId getParentReleaseId() {
            return parentReleaseId;
        }

        @Override
        public Collection<ReleaseId> getDependencies() {
            final Set<ReleaseId> deps = new HashSet<>(32);
            dependencies.entrySet().forEach(scope -> deps.addAll(scope.getValue()));
            return deps;
        }

        public void setParentReleaseId(ReleaseId parentReleaseId) {
            this.parentReleaseId = parentReleaseId;
        }

        protected void addDependency(ReleaseId dependency, String scope) {
            Set<ReleaseId> depsByScope = dependencies.get(scope);
            if (depsByScope == null) {
                depsByScope = new HashSet<ReleaseId>();
                dependencies.put(scope, depsByScope);
            }
            depsByScope.add(dependency);
        }
    }
}
